// 获取应用实例
const app = getApp();

Page({
    data: {
        data_list: [],
        now_page: 0,
        list_total: 0,
        keywords: '',
        limit: 20,
        menu_x: 0,
        menu_y: 0,
        is_show_shade: false,
        is_show_shuaixun: false,
        list_order_method: 2,
        table_head: [
            {name: '单号', field: 'record_sb', is_checked: 0, width: 130},
            {name: '手工单号', field: 'purchase_sn', is_checked: 1, width: 200},
            {name: '供应商', field: 'supply_info', is_checked: 1, width: 220},
            {name: '交期', field: 'dead_line', is_checked: 1, width: 170},
            {name: '采购时间', field: 'confirm_time', is_checked: 1, width: 170},
            {name: '单价', field: 'price', is_checked: 1, width: 150},
            {name: '采购数量', field: 'confirm_num', is_checked: 1, width: 150},
            {name: '收料数量', field: 'reviced_num', is_checked: 1, width: 150},
            {name: '成品简码', field: 'template_no', is_checked: 1, width: 150},
            {name: '到料状态', field: 'reviced_status', is_checked: 1, width: 120},
            {name: '季节', field: 'season', is_checked: 1, width: 150},
        ],
        table_scroll_left: 0,
        left_head_style: "",
        table_head_style: "",
        table_content_style: "",
        max_box_style: "",
        content_header_show: true,
        choose_supplier_name: '',
        supplier_list: [],
        supplier_total: 0,
        supplier_now_page: 0,
        supplier_keywords: '',
        search: [
            {field: 'factory.id', val: -1, operator: '='},
        ],
        is_show_circular: 1,
    },
    onLoad: function (options) {
        var __this = this;
        // 设置标题
        wx.setNavigationBarTitle({
            title: '生产退料单据'
        });
        // 获取采购明细列表
        this.get_list();
        // 设置筛选球位置
        setTimeout(function () {
            // 获取宽高
            __this.setData({
                menu_x: wx.getSystemInfoSync().windowWidth - 60,
                menu_y: wx.getSystemInfoSync().windowHeight - 70,
            });
        }, 100);
        // 获取供应商
        this.get_supplier_list();
    },

    // 每次显示时
    onShow() {
        var __this = this;
        if (!app.check_is_login(false, true)) {
            // 显示登录
            wx.showModal({
                title: '提示',
                content: '请登录后再操作',
                showCancel: false,
                complete() {
                    // 获取采购明细列表
                    __this.get_list();
                    // 获取供应商
                    __this.get_supplier_list();
                }
            });
        }
    },

    // 屏幕旋转事件
    onResize:function(res) {
        console.log(res);
        var is_show_circular = 0;
        if (res.deviceOrientation == 'portrait') {
            is_show_circular = 1;
        }
        this.setData({
            is_show_circular: is_show_circular
        })
    },

    // 页面触底时执行
    onReachBottom: function () {
        this.get_list();
    },

    // 监听下拉[刷新]
    onPullDownRefresh: function () {
        this.setData({
            data_list: [],
            now_page: 0,
            list_total: 0,
            keywords: '',
        });
        // 获取列表
        this.get_list();
        // 停止下拉
        setTimeout(() => {
            wx.stopPullDownRefresh();
        }, 1000)
    },

    // 监听页面滚动
    onPageScroll: function (e) {
        this.setData({
            table_head_style: "",
            table_content_style: "",
            left_head_style: "",
            max_box_style: "",
            content_header_show: true
        })
    },

    // 表格滑动结束
    tr_boxs_scroll_end: function (e) {
        var __this = this;
        setTimeout(function () {
            __this.setData({
                table_scroll_left: e.detail.scrollLeft
            });
        }, 180);
    },

    // 获取收料列表
    get_list: function (is_search = 0) {
        var __this = this;
        // 验证
        if ((this.data.data_list.length >= this.data.list_total) && this.data.now_page > 0) {
            return false;
        }
        if (this.data.search[0].val == -1) {
            this.data.search[0].val = '';
        }
        // 组装请求参数
        var req_data = {
            page: this.data.now_page + 1,
            quick_search: __this.data.keywords,
            limit: __this.data.limit,
            search: this.data.search
        };
        // 发起请求
        var result = app.request_api('admin/SendRefundRecord/index?order=record_id,desc', req_data, true, 'GET');
        result.then(res => {
            console.log(res)
            // 如果是搜索
            if (is_search == 1) {
                this.setData({
                    data_list: [],
                    list_total: 0,
                    now_page: 0
                })
            }
            // 重设
            this.setData({
                data_list: this.data.data_list.concat(res.list),
                list_total: res.total,
                now_page: this.data.now_page + 1
            });
        });
    },

    // 搜索
    GoSearch: function (events) {
        var keywords = events.detail.value.trim();
        this.setData({
            keywords: keywords,
            data_list: [],
            now_page: 0,
            list_total: 0,
        });
        this.get_list(1);
    },

    // 显示筛选
    shuai_xuan_toggle: function () {
        var is_show_shade = true;
        var is_show_shuaixun = true;
        if (this.data.is_show_shuaixun) {
            is_show_shade = false;
            is_show_shuaixun = false;
        }
        this.setData({
            is_show_shade: is_show_shade,
            is_show_shuaixun: is_show_shuaixun,
        });
    },

    // 关闭筛选
    close_shuaixun: function () {
        this.setData({
            is_show_shade: false,
            is_show_shuaixun: false,
        });
    },

    // 选中状态时
    searchStatusChange: function (e) {
        console.log(e);
        var __this = this;
        if (e.detail.value.length > 0) {
            this.data.search[1].val = e.detail.value;
            // 将所有标识未选
            __this.data.status_checkbox_list.forEach(function (item, index) {
                __this.data.status_checkbox_list[index].is_checked = false;
            })
            // 将已选中的标识已选
            e.detail.value.forEach(function (item, index) {
                let s_index = __this.data.status_checkbox_list.findIndex(obj => obj.value === parseInt(item));
                // console.log(s_index)
                if (s_index > -1) {
                    __this.data.status_checkbox_list[s_index].is_checked = true;
                }
            });
        } else {
            this.data.search[1].val = [];
            __this.data.status_checkbox_list.forEach(function (item, index) {
                __this.data.status_checkbox_list[index].is_checked = false;
            })
        }
        this.setData({
            search: this.data.search,
            status_checkbox_list: __this.data.status_checkbox_list
        });
        // 获取数据
        this.get_data_again();
    },

    // 清除供应商
    clearSupplier: function (events) {
        this.data.search[0].val = -1;
        this.setData({
            search: this.data.search,
            choose_supplier_name: ''
        });
        // 获取数据
        this.get_data_again();
    },

    // 获取供应商
    get_supplier_list: function (is_search = 0) {
        var __this = this;
        // 验证
        if ((this.data.supplier_list.length >= this.data.supplier_total) && this.data.supplier_now_page > 0) {
            return false;
        }
        // 发起请求
        var result = app.request_api('admin/Factory/index', {
            page: this.data.supplier_now_page + 1,
            quick_search: __this.data.supplier_keywords,
            limit: 50,
            initKey: 'id',
            initValue: '',
            select: true
        }, 'GET');
        result.then(res => {
            if (is_search == 1) {
                this.setData({
                    supplier_list: [],
                    supplier_now_page: 0,
                    supplier_total: 0
                })
            }
            __this.setData({
                supplier_list: __this.data.supplier_list.concat(res.list),
                supplier_now_page: this.data.supplier_now_page + 1,
                supplier_total: res.total
            })
        });
    },

    // 选择完供应商时
    supplierChange: function (e) {
        console.log(e)
        var choose_supplier_name = '';
        var index = this.data.supplier_list.findIndex(obj => obj.id === parseInt(e.detail.value));
        if (index > -1) {
            choose_supplier_name = this.data.supplier_list[index].SupplyNa;
        }
        this.data.search[0].val = e.detail.value;
        this.setData({
            search: this.data.search,
            choose_supplier_name: choose_supplier_name,
            is_show_shade: false,
            is_show_shuaixun: false,
        });
        // 获取数据
        this.get_data_again();
    },

    // 输入手工单时
    searchInputChange: function (events) {
        console.log(events)
        this.data.search[0].val = events.detail.value.trim();
        this.setData({
            search: this.data.search
        })
        // 获取数据
        this.get_data_again(1);
    },

    // 重新请求数据
    get_data_again: function (is_search = 0) {
        // 重设data
        this.setData({
            data_list: [],
            now_page: 0,
            list_total: 0,
        });
        // 重新调用数据
        this.get_list(is_search);
    },

    // 搜索供应商时
    searchSupplierChange: function (events) {
        var keywords = events.detail.value.trim();
        this.setData({
            supplier_keywords: keywords,
            supplier_list: [],
            supplier_now_page: 0,
            supplier_total: 0,
        });
        this.get_supplier_list(1);
    },

    // 选中是否逾期时
    searchOverdueChange: function (e) {
        console.log(e);
        var s_index = this.data.overdue_checkbox_list.findIndex(obj => obj.value == parseInt(e.detail.value));
        if (s_index > -1) {
            this.data.search[3].val = parseInt(e.detail.value);
            this.setData({
                search: this.data.search
            });
        }
        // 重新请求数据
        this.get_data_again();
    },

    // 改变排序状态
    changeListOrder: function () {
        var list_order_method = 1;
        if (this.data.list_order_method == 1) {
            list_order_method = 2;
        }
        this.setData({
            list_order_method: list_order_method
        });
        // 重新请求数据
        this.get_data_again();
    },

    GoDetail(event) {
        const url = 'send_refund_detail?record_id=' + event.currentTarget.dataset.id + "&sn=" + event.currentTarget.dataset.sn
        wx.navigateTo({'url':url})
    },

})
